﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta><style>/*<![CDATA[*/

table{border: 1px solid gray;}
td{border: 1px dotted gray;}
p{margin: 3px 0 3px 0; padding: 0;}
#ID_Footer{font-size: small; font-style: italic; text-align: right; margin-top: 4em; padding-top: 4px; border-top: 2px solid gray;}

/*]]>*/</style><title>14. ThinkPHP 3.1.2 视图实例演示-登录页面</title></head><body>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%">++++++++++++++++++++++++++++++++++Leo++++++++++++++++++++++++++++++++++++++++</span></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; font-weight: bold; line-height: 140%">项目任务:</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;1. 新建一个Login模块</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;2. 包含验证码，其显示及其刷新、验证</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;3. 用户名密码与数据库中用户名密码匹配</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 240%"> &nbsp; &nbsp; &nbsp; &nbsp;4. 引用css和js文件，及其存放</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; font-weight: bold; line-height: 190%">验证码：</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 190%"> &nbsp; &nbsp;1. 先准备ThinkPHP扩展包 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;非php core包中内容，属于扩展包，需下载解压</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 240%"> &nbsp; &nbsp; &nbsp; &nbsp;ThinkPHP3.1.2_Extend/Extend下的所有内容复制，拷贝到/thinkphp(项目）/ThinkPHP（ThinkPHP核心目录下的Extend文件夹）</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp;2. 生成步骤：</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;a. 在Lib/Action/下新建一个PublicAction.class.php公共模块来生成验证码：</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;class PublicAction extends Action{</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;public function code(){</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; color: #0000ff; line-height: 140%">import(&apos;ORG.Util.Image&apos;);</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Image::buildImageVerify();</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 190%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;b. 在模板中文件中调用, 以显示验证码 </span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 190%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;img</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; color: #0000ff; line-height: 190%"> src=&quot;__APP__/Public/code&quot;/</span><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 190%">&gt;</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;c. 点击验证码图片后刷新功能：</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;img src=&quot;__APP__/Public/code&quot; </span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 140%">onclick</span><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%">=&apos;</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 140%">this.src=this.src</span><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%">&apos;/&gt;</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//单击后自动刷新，IE中不行，应该采用缓存图片，默认URL不变图片即使用缓存，chrome可以刷新，改为如下</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;img src=&quot;__APP__/Public/code&quot; onclick=</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; line-height: 140%">&apos;this.src=this.src+&quot;?&quot;+Math.random()&apos;</span><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> /&gt;</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;d. 验证码检查</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$code = $_POST[&apos;code&apos;];</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if($_SESSION[&apos;verify&apos;] != md5($code)){</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$this-&gt;error(&apos;验证码错误&apos;);</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 240%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} </span></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; font-weight: bold; line-height: 190%">用户名密码验证</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp;1. 在数据库中应具备相应用户名密码数据，选择数据库中User表，点击&quot;结构&quot;，添加一个密码字段</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;名字：password</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;类型：char</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;长度：32 &nbsp; &nbsp; &nbsp; &nbsp;（因为MD5加密后往往是固定的32位长度）</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 190%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;再插入数据</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp;2. 密码比对，即在数据库中查找用户名和密码，看是否存在</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;$m = M(&apos;user&apos;);</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;$where[&apos;username&apos;] = $username;</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;$where[&apos;password&apos;] = $password;</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;$i = $m-&gt;where($where)-&gt;count();</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;if($i &gt; 0){</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$this-&gt;redirect(&apos;User/index&apos;);</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 12pt; font-weight: bold; line-height: 140%">其它</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;页面直跳转 $this-&gt;</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; color: #ff0000; line-height: 140%">redirect</span><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 140%">(&apos;User/index&apos;);</span></div>
<div><span style="font-family: 微软雅黑; font-size: 10pt; line-height: 140%"> &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 微软雅黑; font-size: 10pt; font-weight: bold; color: #ff0000; line-height: 140%">dump</span><span style="font-family: 微软雅黑; font-size: 10pt; color: #000000; line-height: 140%">()函数，ThinkPHP自定义的函数，同PHP的var_dump()</span></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div><script type="text/javascript" language="javascript" src="jquery.js"></script><script type="text/javascript" language="javascript" src="itemlink.js"></script></body></html>